package com.andy.blog.dal.service.impl;

import com.andy.blog.common.exception.BlogServiceException;
import com.andy.blog.common.reponse.BlogResultCode;
import com.andy.blog.common.utils.SecurityUtils;
import com.andy.blog.common.utils.TransformUtils;
import com.andy.blog.dal.domain.ArticleTagDO;
import com.andy.blog.dal.mapper.ArticleTagMapper;
import com.andy.blog.dal.service.ArticleTagDalService;
import com.andy.blog.dal.vo.ArticleTagVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * Created by andy on 2023/7/3 17:11
 */
@Service
public class ArticleTagDalServiceImpl implements ArticleTagDalService {

    @Resource
    private ArticleTagMapper articleTagMapper;

    /**
     * 构建查询条件
     */
    private LambdaQueryWrapper<ArticleTagDO> buildQueryWrapper(ArticleTagVO articleTagVO) {
        return new LambdaQueryWrapper<ArticleTagDO>()
                .eq(Objects.nonNull(articleTagVO.getArticleId()), ArticleTagDO::getArticleId, articleTagVO.getArticleId())
                .in(CollectionUtils.isNotEmpty(articleTagVO.getArticleIdList()), ArticleTagDO::getArticleId, articleTagVO.getArticleIdList());
    }

    @Override
    public void insertArticleTag(ArticleTagVO articleTagVO) {
        ArticleTagDO articleTagDO = TransformUtils.transform(articleTagVO, ArticleTagDO.class);
        if (null == articleTagDO) {
            throw new BlogServiceException(BlogResultCode.BLOG_000001, "入参不能为空");
        }

        articleTagDO.setCreateBy(SecurityUtils.getUsername());

        int row;
        try {
            row = articleTagMapper.insert(articleTagDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100, e);
        }

        if (row <= 0) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100);
        }
    }

    @Override
    public List<ArticleTagDO> selectList(ArticleTagVO articleTagVO) {
        LambdaQueryWrapper<ArticleTagDO> lambdaQueryWrapper = buildQueryWrapper(articleTagVO);
        return articleTagMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public List<ArticleTagDO> selectBatchIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Lists.newArrayList();
        }
        return articleTagMapper.selectBatchIds(ids);
    }

    @Override
    public void deleteByArticleId(Long articleId) {
        if (Objects.isNull(articleId)) {
            throw new BlogServiceException("文章ID不能为空");
        }

        LambdaQueryWrapper<ArticleTagDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ArticleTagDO::getArticleId, articleId);

        int row;
        try {
            row = articleTagMapper.delete(lambdaQueryWrapper);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000102, e);
        }

        if (row <= 0) {
            throw new BlogServiceException(BlogResultCode.BLOG_000102);
        }
    }

    @Override
    public Long selectCount(ArticleTagVO articleTagVO) {
        LambdaQueryWrapper<ArticleTagDO> lambdaQueryWrapper = buildQueryWrapper(articleTagVO);

        return articleTagMapper.selectCount(lambdaQueryWrapper);
    }
}
